package com.aletto.general_store.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;

import androidx.annotation.NonNull;

import com.youth.banner.indicator.BaseIndicator;

public class GoodsCoverIndicator extends BaseIndicator {

    private int mNormalRadius;
    private int mSelectedRadius;
    private int maxRadius;
    private RectF backgroundRectF;
    private final Paint backgroundPaint;

    public GoodsCoverIndicator(Context context) {
        this(context, null);
    }

    public GoodsCoverIndicator(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GoodsCoverIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mNormalRadius = config.getNormalWidth() / 2;
        mSelectedRadius = config.getSelectedWidth() / 2;

        backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        backgroundPaint.setColor(Color.parseColor("#20202020"));

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int count = config.getIndicatorSize();
        if (count <= 1) {
            return;
        }

        mNormalRadius = config.getNormalWidth() / 2;
        mSelectedRadius = config.getSelectedWidth() / 2;

        // 考虑当 选中和默认 的大小不一样的情况
        maxRadius = Math.max(mSelectedRadius, mNormalRadius);

        // 间距*（总数-1）+选中宽度+默认宽度*（总数-1）
        int width = (count - 1) * config.getIndicatorSpace() + config.getSelectedWidth() + config.getNormalWidth() * (count - 1);

        setMeasuredDimension(
                width + dp2px(16),
                Math.max(config.getNormalWidth(), config.getSelectedWidth()) + dp2px(8)
        );

        if (backgroundRectF == null) {
            backgroundRectF = new RectF(
                    0,
                    0,
                    width + dp2px(16),
                    Math.max(config.getNormalWidth(), config.getSelectedWidth()) + dp2px(8)
            );
        }
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);

        if (backgroundRectF == null) return;

        // 绘制背景
        canvas.drawRoundRect(backgroundRectF, dp2px(10), dp2px(10), backgroundPaint);

        int count = config.getIndicatorSize();
        if (count <= 1) {
            return;
        }

        float left = 0;
        for (int i = 0; i < count; i++) {
            mPaint.setColor(config.getCurrentPosition() == i ? config.getSelectedColor() : config.getNormalColor());
            int indicatorWidth = config.getCurrentPosition() == i ? config.getSelectedWidth() : config.getNormalWidth();
            int radius = config.getCurrentPosition() == i ? mSelectedRadius : mNormalRadius;

            if (i == 0) {
                canvas.drawCircle(left + dp2px(8) + radius, maxRadius + dp2px(4), radius, mPaint);
                left += indicatorWidth + config.getIndicatorSpace() + dp2px(8);

            } else {
                canvas.drawCircle(left + radius, maxRadius + dp2px(4), radius, mPaint);
                left += indicatorWidth + config.getIndicatorSpace();
            }
        }
    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, Resources.getSystem().getDisplayMetrics());
    }

}
